package com.nebula.tlggoods.service.impl;

import com.github.pagehelper.PageHelper;
import com.nebula.tlggoods.entity.Shop;
import com.nebula.tlggoods.mapper.ShopMapper;
import com.nebula.tlggoods.service.ShopService;
import com.nebula.tlggoods.tools.Record;
import com.nebula.tlggoods.tools.Status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 店铺实现类
 */
@Service
public class ShopServiceImpl implements ShopService {

    @Resource
    private ShopMapper shopMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 统计店铺数量
     *
     * @param keyword 关键字
     * @return
     */
    @Override
    public Record countShopQuantity(String keyword) {
        Record record = new Record();
        Integer count = null;
        String key = "shopQuantity" + keyword;
        ValueOperations operations = redisTemplate.opsForValue();
        try {
            //缓存存在
            if (redisTemplate.hasKey(key)) {
                count = (int) operations.get(key);
            } else {
                count = shopMapper.countShopQuantity(keyword);
                //添加缓存 3分钟
                operations.set(key, count, 3, TimeUnit.MINUTES);
            }
        } catch (Exception e) {
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("统计店铺数量异常！！！");
            e.printStackTrace();
            return record;
        }
        if (null != count) {
            record.setStatus(Status.SELECT_SUCCESS);
            record.setMsg("统计店铺数量成功");
            record.setData(count);
        } else {
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("统计店铺数量失败！！");
        }
        return record;
    }


    /**
     * 模糊查询并分页、排序
     *
     * @param pageNum  页码
     * @param pageSize 每页显示条数
     * @param keyword  关键字
     * @return
     */
    @Override
    public Record findAllByFuzzyNameAndPaging(Integer pageNum, Integer pageSize, String keyword) {
        Record record = new Record();
        if (null == pageNum) {
            pageNum = 1;
        }
        if (null == pageSize) {
            pageSize = 10;
        }
        List<Shop> list = null;
        String key = "shopPagingpageNum" + pageNum + "pageSize" + pageSize + "keyword" + keyword;
        ValueOperations<String, List<Shop>> operations = redisTemplate.opsForValue();
        try {
            PageHelper.startPage(pageNum, pageSize);
            //缓存存在
            if (redisTemplate.hasKey(key)) {
                list = operations.get(key);
            } else {
                list = shopMapper.findAllByFuzzyNameAndPaging(keyword);
                //添加缓存  5分钟
                operations.set(key, list, 5, TimeUnit.MINUTES);
            }
        } catch (Exception e) {
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("店铺列表查询异常！！！");
            e.printStackTrace();
            return record;
        }
        if (null != list && list.size() > 0) {
            record.setStatus(Status.SELECT_SUCCESS);
            record.setMsg("店铺列表查询成功");
            record.setData(list);
        } else {
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("店铺列表查询失败！！");
        }
        return record;
    }
}
